<?php
$imagesdir = GALLERY_DIR;
$pictures = array();
if (is_dir($imagesdir) && $dh = opendir($imagesdir)) {
	$files = array();
	while (($file = readdir($dh)) !== false) {
		if (strpos($file, ".jpg") !== false && strpos($file, "tb_") === false) {
			$files[] = $file;
		}
	}
	closedir($dh);
	sort($files);

	$numfiles = count($files);
	$maxpages = ceil($numfiles / PHOTOS_PER_PAGE);
	$page = isset($_GET["page"]) ? $_GET["page"] : 1;
	if ($page < 1) $page = 1;
	if ($page > $maxpages) $page = $maxpages;

	$navpages = array();
	if ($page > 1) {
		$navpages[] = array(
			"number" => "<",
			"link" => 1,
			"url" => Utils::getBaseUrl($lang, "gallery").($page - 1)."/"
		);
	}
	for ($i=1; $i<=$maxpages; $i++) {
		$navpages[] = array(
			"number" => $i,
			"link" => $i == $page ? 0 : 1,
			"url" => Utils::getBaseUrl($lang, "gallery").$i."/"
		);
	}
	if ($page < $maxpages) {
		$navpages[] = array(
			"number" => ">",
			"link" => 1,
			"url" => Utils::getBaseUrl($lang, "gallery").($page + 1)."/"
		);
	}
	$content->assign("navpages", $navpages);

	$last = PHOTOS_PER_PAGE * $page;
	$first = $last - PHOTOS_PER_PAGE + 1;

	$files2 = array();
	for ($i=$first; $i<=$last; $i++) {
		if (!empty($files[$i-1])) {
			$files2[] = $files[$i-1];
		}
	}

	foreach ($files2 as $file) {
		$url = $content->getBasePath().GALLERY_URL.$file;
		$pictures[] = array(
			"url" => $url,
			"thumb" => $content->getBasePath()."thumb.php?s=gallery&f=".$file."&w=140&h=85",
			"name" => $file
		);
	}

	$content->assign("page", $content->langmsg["page"]);
}
$content->assign("pictures", $pictures);
?>
